﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KFGame;
using System;


//提交设备信息
public class DeviceInfo : KFTask
{
    // Start is called before the first frame update 
    public string server = "http://game.qualityperson.net";

    public string gname = "yysc";
    string url = "/api/index/collectdeviceinfo";
    string ctag = "deviceinfo";


    void Awake()
    {
        once = true;

    }

    public override void task()
    {
        sendMessage();
    }

    public void sendMessage()
    {
        GameConfigData datas = KF.kf.config;
        //现在时间
        DateTime ntime = DateTime.Now;

        //上一次发送时间
        DateTime ptime = Convert.ToDateTime(datas.reocedtime);
        TimeSpan ts = ntime.Subtract(ptime);
        //超过一小时发一次
        if (ts.Days == 0 && ts.Hours == 0)
            return;

        newreoced_coins = datas.coins_all;
        newreoced_tickes = datas.tickes_all;

        double sendcoins = datas.coins_all - datas.reoced_coins;
        double sendtickets = datas.tickes_all - datas.reoced_tickes;


        authorization author = new authorization();
        string dcode = author.getDivecCode();
        string slats = author.markCode();


        AuConfig ac = authorization.LoadConfigDatas();

        string mdata = ac.deviceId + dcode + slats + sendcoins.ToString() + datas.run_game + sendtickets.ToString() + ntime.ToString();

        WWWForm form = new WWWForm();
        form.AddField("device_id", ac.deviceId);
        form.AddField("dcode", dcode);
        form.AddField("runtime", 0);
        form.AddField("coin", sendcoins.ToString());
        form.AddField("car", 0);
        form.AddField("type", 0);
        form.AddField("gcount", datas.run_game);
        form.AddField("tickets", sendtickets.ToString());
        form.AddField("stime", ntime.ToString());
        form.AddField("mask", authorization.getMark(mdata));


        KF.kf.netWorkManager.Post(ctag, server + url, form);
    }
    double newreoced_coins = 0;
    double newreoced_tickes = 0;

    void saveRecode()
    {


        GameConfigData datas = KF.kf.config;
        //现在时间
        DateTime ntime = DateTime.Now;
        datas.reocedtime = ntime.ToString();
        datas.reoced_coins = newreoced_coins;
        datas.reoced_tickes = newreoced_tickes;
        datas.save();
    }


    void Event(string tag, string url, int id, string message)
    {
        Debug.Log("net call back :" + tag + ":" + id);
        if (tag != ctag)
            return;

        if (id == 404)
        {
            finish = true;
            return;
        }

        if (id != 200)
            return;


        var json = new MiniJSON2.JSON();
        json.serialized = message;

        if (json.ToInt("code") != 1)
            return;
        //成功反回
        saveRecode();

        finish = true;
    }
    // Update is called once per frame
    void Update()
    {

    }
}
